<template>
  <div>
    <div style="margin: 10px 0">
      <el-input v-model="workerName" style="width: 200px" placeholder="请输入顾客名字" suffix-icon="el-icon-position"
        class="mll-5" />
      <el-button class="mll-5" type="primary" @click="findApi">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
      <el-button type="primary" @click="handleAdd">新增<i class="el-icon-circle-plus-outline" /></el-button>
    </div>
    <el-card class="box-card">
      <el-table :data="workerData" style="width: 100%">
        <el-table-column label="员工ID" prop="workerId" />
        <el-table-column label="姓名" prop="workerName" />
        <el-table-column label="用户名" prop="username" />
        <el-table-column label="工资" prop="workerSalary" />
        <el-table-column label="性别" prop="workerSex">
          <template #default="scope">
            <span v-if="scope.row.workerSex == 1">男</span>
            <span v-else-if="scope.row.workerSex == 2">女</span>
            <span v-else-if="scope.row.workerSex == 0">?</span>
          </template>
        </el-table-column>

        <el-table-column label="年龄" prop="workerAge" />
        <el-table-column label="入职时间" prop="entrydate" />

        <el-table-column label="创建时间" prop="createTime" />
        <el-table-column label="更新时间" prop="updateTime" />
        <el-table-column label="职位" prop="workerCategory">
          <template #default="scope">
            <span v-if="scope.row.workerCategory == 1">经理</span>
            <span v-else-if="scope.row.workerCategory == 2">厨师</span>
            <span v-else-if="scope.row.workerCategory == 3">服务员</span>

          </template>
        </el-table-column>
        <el-table-column align="right" width="150">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.workerId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <span class="demonstration" />
        <div style="padding: 10px 0; text-align: center;">
          <el-pagination :current-page="pageNum" :page-sizes="[5, 8, 10, 20]" :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
        </div>
      </div>

      <el-dialog title="员工信息" :visible.sync="dialogFormVisible" width="50%">
        <el-form label-width="80px" size="small">
          <el-form-item label="姓名">
            <el-input v-model="form.workerName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="用户名">
            <el-input v-model="form.username" autocomplete="off" />
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="form.workerSex" placeholder="请选择性别">
              <el-option label="男" value="1"></el-option>
              <el-option label="女" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="form.workerAge" autocomplete="off" />
          </el-form-item>
          <el-form-item label="工资">
            <el-input v-model="form.workerSalary" autocomplete="off" />
          </el-form-item>

          <el-form-item label="职位">
            <el-select v-model="form.workerCategory" placeholder="请选择职位">
              <el-option label="经理" value="1"></el-option>
              <el-option label="厨师" value="2"></el-option>
              <el-option label="服务员" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="入职时间">
            <!-- <el-input v-model="form.entrydate" autocomplete="off" /> -->
            <div class="block">
              <span class="demonstration">默认</span>
              <el-date-picker v-model="form.entrydate" type="date" placeholder="选择日期" />
            </div>

          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleSave">{{ isEditMode ? '修改' : '确定' }}</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>
<!-- customerId,customerName,customerPhone,customerSex -->
 <!-- workerId,username,password,workerName,workerSalary,workerSex,workerAge,entrydate,createTime,updateTime,workerCategory -->
<script>
import service from '@/utils/request'
export default {
  data() {
    return {
      workerData: [],
      search: '',
      pageNum: 1,
      pageSize: 5,
      total: 0,
      workerName: '',
      form: {},
      dialogFormVisible: false,
      fileList: [],
      isEditMode: false,
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        },
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }]
      }
    }
  },
  // workerId,username,password,workerName,workerSalary,workerSex,workerAge,entrydate,createTime,updateTime,workerCategory
  // customerId;customerName;customerPhone;customerSex;
  created() {
    this.fetchDataFromApi()
  },
  methods: {
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.fetchDataFromApi()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.fetchDataFromApi()
    },
    fetchDataFromApi() {
      service.get('http://localhost:9090/user/worker?page=' + this.pageNum + '&pageSize=' + this.pageSize + '&name=' + this.workerName)
        .then(res => {
          this.total = res.data.total
          this.workerData = res.data.rows
        })
        .catch(error => {
          console.error('Error fetching data from API:', error)
        })
    },
    findApi() {
      service.get('http://localhost:9090/user/worker?name=' + this.workerName)
        .then(res => {
          this.total = res.data.total
          this.workerData = res.data.rows
        })
        .catch(error => {
          console.error('Error fetching data from API:', error)
        })
    },
    reset() {
      this.workerName = ''
      this.fetchDataFromApi()
    },
    handleSave() {
      if (this.isEditMode) {
        this.updata()
      } else {
        this.save()
      }
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    save() {
      service.post('http://localhost:9090/user/worker', this.form).then(res => {
        if (res) {
          this.$message.success('保存成功')
          this.dialogFormVisible = false
          this.fetchDataFromApi()
        } else {
          this.$message.error('保存失败')
        }
      })
    },
    delete(workerId) {
      service.delete('http://localhost:9090/user/worker/' + workerId).then(res => {
        if (res) {
          this.$message.success('删除成功')
          this.delVisibleaa = false
          this.fetchDataFromApi()
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    handleEdit(row) {
      this.form = row
      // if(this.form.workerSex == 1){
      //   this.form.workerSex = "男"
      // }
      // if(this.form.workerSex == 2){
      //   this.form.workerSex = "女"
      // }
      // if(this.form.workerCategory == 1){
      //   this.form.workerCategory = "经理"
      // }
      // if(this.form.workerCategory == 2){
      //   this.form.workerCategory = "厨师"
      // }
      // if(this.form.workerCategory == 3){
      //   this.form.workerCategory = "服务员"
      // }
      this.dialogFormVisible = true
      this.isEditMode = true
    },
    handleDelete(workerId) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.delete(workerId)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    updata() {
      service.put('http://localhost:9090/user/worker', this.form).then(res => {
        if (res) {
          this.$message.success('保存成功')
          this.dialogFormVisible = false
          this.fetchDataFromApi()
        } else {
          this.$message.error('保存失败')
        }
      })
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.mll-5 {
  margin-left: 10px;
}
</style>
